
#include "qjson.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonDocument>
#include <QFile>

qjson::qjson()
{

}
/*
{
    "Name":"Ace",
    "Sex":"man",
    "Age":20,
    "Family":{
        "Father":"Gol·D·Roger",
        "Mother":"Portgas·D·Rouge",
        "Brother":["Sabo", "Monkey D. Luffy"]
    },
    "IsAlive":false,
    "Comment":"yyds"
}
*/
void qjson::WriteJson(){
    QJsonObject json_obj;
    json_obj.insert("name","Ace");   //qjsonvalue 里面有隐式类型转换
    json_obj.insert("sex","man");
    json_obj.insert("age","20");

    QJsonObject json_obj_family;
    json_obj_family.insert("father","Gol·D·Roger");
    json_obj_family.insert("Mother","Portgas·D·Rouge");
    QJsonArray json_obj_family_brother;
    json_obj_family_brother.append("Sabo");
    json_obj_family_brother.append("Monkey D. Luffy");
    json_obj_family.insert("brother",json_obj_family_brother);
    json_obj.insert("family",json_obj_family);
    json_obj.insert("IsAlive",false);
    json_obj.insert("Comment","YYDS");

    QJsonDocument doc(json_obj);
    QByteArray json = doc.toJson();   //tobytearray方法转的就是二进制文件了

    QFile file ("D:\\bbbbbbcccccccccc\\QT_code\\QJsonTest\\ace.json");
    file.open(QFile::WriteOnly);
    file.write(json);  //write里面可以是char* 和bytearray对象
    file.close();
}

void qjson::ReadJson(){
    QFile file ("D:\\bbbbbbcccccccccc\\QT_code\\QJsonTest\\ace.json");
    file.open(QFile::ReadOnly);
    QByteArray all = file.readAll();
    file.close();

    QJsonDocument doc = QJsonDocument::fromJson(all);
    if(doc.isObject())
    {
        QJsonObject json_obj = doc.object();
        QStringList keys = json_obj.keys();
        for(int i=0;  i<keys.size() ; ++i)
        {
            QString key =keys.at(i);
            QJsonValue value = json_obj.value(key);
            if(value.isBool())
            {
                qDebug()<<key <<":"<<value.toBool();
            }
            else if(value.isDouble())
            {
                qDebug()<<key <<":"<<value.toInt();
            }
            else if(value.isString())
            {
                qDebug()<<key <<":"<<value.toString();
            }
            else if(value.isObject())
            {
                qDebug()<<key <<":";
                QJsonObject sub_OBJ = value.toObject();
                QStringList sub_keys = sub_OBJ.keys();
                for (int k = 0; k < sub_keys.size(); ++k) {
                    QJsonValue sub_value = sub_OBJ.value(sub_keys.at(k));
                    if(sub_value.isString())
                    {
                        qDebug() <<"  "<<sub_keys.at(k) <<":"<<sub_value.toString();
                    }
                    else if(sub_value.isArray())
                    {
                        QJsonArray array = sub_value.toArray();
                        for (int j = 0; j < array.size(); ++j) {
                            qDebug()<<"brother:"<<array[j].toString();
                        }
                    }
                }
            }
        }
    }
}
